#include <linux/init.h>         /* printk() */   
#include <linux/module.h>       /* __init __exit */   
#include <linux/delay.h>
#include <linux/spi/spi.h>
#include <linux/spi/eeprom.h>
#include <linux/property.h>


static int eeprom_probe(struct spi_device *spi)
{
	u32 val;
	u8  command[32];
	u8 buf[20];
	u8 *cp;
	struct spi_transfer t[2];
	struct spi_message m;
	int sr;
	dev_info(&spi->dev, "hello, driver probed.\n");

	if (device_property_read_u32(&spi->dev, "pagesize", &val) == 0) {
		dev_info(&spi->dev, "page size: %d\n", val);
	}

	sr = spi_w8r8(spi, 0x05);
	if (sr < 0 || sr & 0x01) 
		dev_info(&spi->dev, "not ready: %d\n", sr);
	else 
		dev_info(&spi->dev, "ready: %d\n", sr);

	memset(command, 0, sizeof(command));
	memset(buf, 0, sizeof(buf));

	cp = command;
	*cp++ = 0x03;

	spi_message_init(&m);
	memset(t, 0, sizeof(t));

	t[0].tx_buf = command;
	t[0].len = 3;
	spi_message_add_tail(&t[0], &m);

	t[1].rx_buf = buf;
	t[1].len = 16;
	spi_message_add_tail(&t[1], &m);

	sr = spi_sync(spi, &m);
	dev_info(&spi->dev, "read status %zd\n", sr);
	dev_info(&spi->dev, "buf: %s\n", buf);


	return 0;
}

static const struct of_device_id dummy_of_match[] = {
	{.compatible = "meng,eeprom"},
	{ }
};
MODULE_DEVICE_TABLE(of, dummy_of_match);

static struct spi_driver dummy_driver = {
	.driver = {
		.name = "eeprom",
		.of_match_table = dummy_of_match,
	},
	.probe = eeprom_probe,

};

module_spi_driver(dummy_driver);

MODULE_AUTHOR("Meng"); 
MODULE_LICENSE("GPL");
  
MODULE_DESCRIPTION("A simple Hello World Module");
MODULE_ALIAS("a simplest module");
